package com.gopay.webtest.controller;

import com.gopay.webtest.handler.Trans4010Handler;
import com.gopay.webtest.handler.Trans4011Handler;
import com.gopay.webtest.vo.GoodsInfo;
import com.gopay.webtest.xml.XmlDao;
import com.gopay.webtest.controller.base.BaseController;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/goods")
public class GoodsReturnController extends BaseController {

	private static Logger logger = Logger.getLogger(GoodsReturnController.class);
	private static String GOODS_RETURN_XML = "goodsreturn";
	private static String RETURN_TRANSFER_XML = "return_transfer";
	private static String GOODSINFO = "goodsinfo";
	
	@RequestMapping(method = RequestMethod.GET, value = "/return")
	public ModelAndView returnGoods() {

		ModelAndView mv = new ModelAndView("gopay.webtest.pay.return_goods");

		// 加载xml数据到
		try {
			Map<String, String> map = this.xmlDao.readData4Map(GOODS_RETURN_XML,
					GOODSINFO);
			mv.addObject("goodsinfo", map);
			mv.setViewName("gopay.webtest.pay.return_goods");
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mv;
	}

	@RequestMapping("/return/submit")
	public ModelAndView addReturnGoods(GoodsInfo goodsinfo) {

		ModelAndView mv = new ModelAndView(
				"gopay.webtest.pay.return_goods_submit");

		/*** 对表单数据进行处理 begin */

		try {
			// 进行加签
			trans4010Handler.sign(goodsinfo.getTranCode(), "signType",
					"signValue", goodsinfo, getRequest());
		} catch (Exception e) {

			logger.error("加签出错");
			e.printStackTrace();
		}
		mv.setViewName("gopay.webtest.pay.return_goods_submit");
		mv.addObject("goodsinfo",goodsinfo);
		return mv;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/returnTransfer")
	public ModelAndView returnTransfer() {

		ModelAndView mv = new ModelAndView("gopay.webtest.pay.return_transfer");

		// 加载xml数据到
		try {
			Map<String, String> map = this.xmlDao.readData4Map(RETURN_TRANSFER_XML,GOODSINFO);
			mv.addObject("goodsinfo", map);
			mv.setViewName("gopay.webtest.pay.return_transfer");
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mv;
	}

	@RequestMapping("/returnTransfer/submit")
	public ModelAndView submitReturnTransfer(GoodsInfo goodsinfo) {

		ModelAndView mv = new ModelAndView("gopay.webtest.pay.return_transfer_submit");

		/*** 对表单数据进行处理 begin */

		try {
			// 进行加签
			trans4011Handler.sign(goodsinfo.getTranCode(), "signType","signValue", goodsinfo,this.getRequest());
		} catch (Exception e) {
			logger.error("加签出错");
			e.printStackTrace();
		}
		mv.setViewName("gopay.webtest.pay.return_transfer_submit");
		mv.addObject("goodsinfo",goodsinfo);
		return mv;
	}

	private XmlDao xmlDao = null;

	private Trans4010Handler trans4010Handler;
	private Trans4011Handler trans4011Handler;

	public XmlDao getXmlDao() {
		return xmlDao;
	}

	@Resource(name = "xmlDao")
	public void setXmlDao(XmlDao xmlDao) {
		this.xmlDao = xmlDao;
	}

	public Trans4010Handler getTrans4010Handler() {
		return trans4010Handler;
	}

	@Resource(name="trans4010Handler")
	public void setTrans4010Handler(Trans4010Handler trans4010Handler) {
		this.trans4010Handler = trans4010Handler;
	}

	public Trans4011Handler getTrans4011Handler() {
		return trans4011Handler;
	}

	@Resource(name="trans4011Handler")
	public void setTrans4011Handler(Trans4011Handler trans4011Handler) {
		this.trans4011Handler = trans4011Handler;
	}

}
